// pages/wuliurongqiguanli/nav_check_gzcbl/check_gzcbl.js
const mybehavior = require("../../../../../behaviors/requests")
import WxValidate from "../../../../../Wx_Validate/WxValidate"
import Toast from '../../../../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    company: "珠海格力电器股份有限公司",
    operation: "",
    order: "",
    material_name: "",
    manager: "",
    line: "",
    options1: ["珠海格力电器股份有限公司",],
    options2: [],
    options3: [],
    options4: [],
    options5: [],
    options6: [],
    options7: [],
    options8: [],
    popup_show1: false,
    popup_show2: false,
    popup_show3: false,
    popup_show4: false,
    popup_show5: false,
    popup_show6: false,
    popup_show7: false,
    material_no: "",
    baseUrl: "https://logisticsengineering.gree.com/order_system",
    list: [],
    btn_type: "danger",
    btn_type1: "default",
    submit_disabled: false
  },

  // 下拉选择
  getPickerVlaue(e) {
    console.log(e);
    let key = e.target.id
    this.setData({
      [key]: e.detail,
    })
    if (key == "manager") {
      this.getLineData()
      this.setData({
        line: "",
        operation: "",
        material_name: "",
        material_no: "",
        detail: "",
        order: "",
      })

    }
    if (key == "line") {
      this.getLineData()
      this.getOperationsData()
      this.getMachineTypeData()
      this.setData({
        operation: "",
        material_name: "",
        material_no: "",
        detail: "",
        order: "",
        options5: [],
      })

    }
    if (key == "operation") {
      this.getJobNameData()

    }
    if (key == "order") {
      this.setData({
        [key]: e.detail._id,
      })
      this.getMachineData()

    }
    if (key == "detail") {
      this.getOrderData()
      this.setData({
        [key]: e.detail.detail_code,
        order: "",
      })
    }
  },
  scanPickerValue(e) {
    let key = e.target.id
    this.setData({
      [key]: e.detail,
    })

  },
  // 报完工按钮
  bwgBtn() {
    wx.showToast({
      title: '未开放',
      icon: "error"
    })
    // wx.redirectTo({
    //   url: '/packageB/pages/package/lvkon/baowangon/baowangon',
    // })
  },
  btn1() {
    let type = this.data.btn_type
    if (type != "danger") {
      this.setData({
        btn_type: "danger",
        btn_type1: "default",
      })
      console.log("再叫料");
      wx.redirectTo({
        url: '/packageB/pages/package/lvkon/jiaoliao/jiaoliao',
      })
    }

  },
  btn2() {
    wx.showToast({
      title: '未开放',
      icon: "error"
    })
    // let type = this.data.btn_type1
    // if (type != "danger") {
    //   this.setData({
    //     btn_type1: "danger",
    //     btn_type: "default",
    //   })
    //   console.log("已叫料");
    //   wx.redirectTo({
    //     url: '/packageB/pages/package/lvkon/yijiaoliao/yijiaoliao',
    //   })
    // }

  },
  // 扫描
  scan1() {
    const self = this;
    wx.scanCode().then((res) => {
      const label_code = res.result;
      wx.showLoading({
        title: "loading"
      })
      console.log(label_code);
      setTimeout(function () {
        wx.hideLoading()
        self.setData({
          material_no: label_code
        })
      }, 500)
    }).catch(err => {
      console.log(err);
    });
  },

  // 查询--公司、单位--集合
  getFactoryData() {
    let url = this.data.baseUrl + "/get_u_permission";
    let data = {}

    this.post(url, data).then(res => {
      // console.log("返回结果", res)
      if (res.code == 20000) {

        this.setData({
          options6: res.data.珠海格力电器股份有限公司,
        })
        // wx.showToast({
        //   title: res.msg,
        //   icon: "success"
        // });
      } else {
        this.setData({
          options6: [],
        })
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      this.setData({
        options6: [],
      })
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 查询--叫料点的线体--集合
  getLineData() {
    let url = this.data.baseUrl + "/positions_lines";
    let data = {
      company: this.data.company,
      manager: this.data.manager,
    }

    this.post(url, data).then(res => {
      console.log("返回结果", res)
      if (res.code == 20000) {

        this.setData({
          options7: res.data,
        })
        // wx.showToast({
        //   title: res.msg,
        //   icon: "success"
        // });
      } else {
        this.setData({
          options7: [],
        })
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      this.setData({
        options7: [],
      })
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  //  // 查询--工序段--集合
  getOperationsData() {
    let url = this.data.baseUrl + "/positions_operations";
    let data = {
      company: this.data.company,
      manager: this.data.manager,
      line: this.data.line,
    }

    this.post(url, data).then(res => {
      console.log("返回结果", res)
      if (res.code == 20000) {

        this.setData({
          options2: res.data,
        })
        // wx.showToast({
        //   title: res.msg,
        //   icon: "success"
        // });
      } else {
        this.setData({
          options2: [],
        })
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      this.setData({
        options2: [],
      })
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  //  // 查询--物料名称--集合
  getJobNameData() {
    let url = this.data.baseUrl + "/positions_job_names";
    let data = {
      company: this.data.company,
      manager: this.data.manager,
      line: this.data.line,
      operation: this.data.operation,
    }

    this.post(url, data).then(res => {
      console.log("返回结果", res)
      if (res.code == 20000) {

        this.setData({
          options3: res.data,
        })
        // wx.showToast({
        //   title: res.msg,
        //   icon: "success"
        // });
      } else {
        this.setData({
          options3: [],
        })
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      this.setData({
        options3: [],
      })
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  //  // 查询--通过物料编码查物料名称--集合
  getMaterialNameData() {
    let url = this.data.baseUrl + "/get_material_name_by_no";
    let data = {
      material_no: this.data.material_no,

    }

    this.post(url, data).then(res => {
      console.log("通过物料编码查物料名称", res)
      if (res.code == 20000) {
        let val = res.data.material_name

        this.setData({
          material_name: val,
        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  //  // 查询--机型--集合
  getMachineTypeData() {
    let url = this.data.baseUrl + "/line_detail_order";
    let data = {
      company: this.data.company,
      manager: this.data.manager,
      line: this.data.line,
      detail: "",
      order: "",
    }

    this.post(url, data).then(res => {
      console.log("机型返回结果", res)
      if (res.code == 20000) {
        let arr = res.data.results
        for (let i = 0; i < arr.length; i++) {
          for (let j = i + 1; j < arr.length; j++) {
            if (arr[i].detail_code == arr[j].detail_code) {
              arr.splice(j, 1);
              j--;
            }
          }
        }
        this.setData({
          options4: arr,
        })
        // wx.showToast({
        //   title: res.msg,
        //   icon: "success"
        // });
      } else {
        this.setData({
          options4: [],
        })
        // wx.showToast({
        //   title: res.msg,
        //   icon: "error"
        // })
      }
    }).catch(err => {
      console.log(err);
      this.setData({
        options4: [],
      })
      // wx.showToast({
      //   title: err.msg,
      //   icon: 'error'
      // })
    })
  },
  //  // 查询--通过机型查订单--集合
  getOrderData() {
    let url = this.data.baseUrl + "/line_detail_order";
    let data = {
      company: this.data.company,
      manager: this.data.manager,
      line: this.data.line,
      detail: this.data.detail.detail_code,
      order: "",
    }

    this.post(url, data).then(res => {
      console.log("通过机型查订单", res)
      if (res.code == 20000) {
        let arr = res.data.results

        this.setData({
          options5: arr,
          options8: arr,
        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  //  // 查询--通过订单查机型--集合
  getMachineData() {
    let url = this.data.baseUrl + "/line_detail_order";
    let data = {
      company: this.data.company,
      manager: this.data.manager,
      line: this.data.line,
      detail: "",
      order: this.data.order,
    }

    this.post(url, data).then(res => {
      console.log("通过订单查机型", res)
      if (res.code == 20000) {
        let arr = res.data.results

        this.setData({
          detail: arr[0].detail_code,
          order: this.data.order_no,
          popup_show5: false,
        })

      } else {
        // wx.showToast({
        //   title: res.msg,
        //   icon: "error"
        // })
      }
    }).catch(err => {
      // wx.showToast({
      //   title: err.msg,
      //   icon: 'error'
      // })
    })
  },

  //初始化表单验证规则
  initValidate() {
    let rules = {
      company: {
        required: true,
      },
      manager: {
        required: true,
      },

      line: {
        required: true,
      },
      operation: {
        required: true,
      },
      material_name: {
        required: true,
      },
      order: {
        required: true,
      },


    }

    let message = {
      company: {
        required: "公司不能为空",
      },
      manager: {
        required: "单位不能为空",
      },

      line: {
        required: "产线选取不能为空",
      },
      operation: {
        required: "工序段不能为空",
      },

      material_name: {
        required: "物料名称不能为空",
      },


      order: {
        required: "订单选取不能为空",
      },



    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  // 判断有无物料编码
  submit() {
    this.setData({
      submit_disabled: true
    })
    let that = this
    let data = {
      company: that.data.company,
      manager: that.data.manager,
      line: that.data.line,
      operation: that.data.operation,
      material_name: that.data.material_name,
      order: that.data.order,
    }
    if (!that.WxValidate.checkForm(data)) {
      const error = that.WxValidate.errorList[0]
      Toast(error.msg)
      this.setData({
        submit_disabled: false
      })
    } else {
      let val = that.data.material_no
      if (val == "") {

        that.submit2()
        this.setData({
          submit_disabled: false
        })
      } else {
        that.submit1()
        this.setData({
          submit_disabled: false
        })
      }
    }


  },
  //  包含物料编码叫料
  submit1() {
    let url = this.data.baseUrl + "/call_material_by_no";
    let data = {
      company: this.data.company,
      manager: this.data.manager,
      line: this.data.line,
      operation: this.data.operation,
      material_num: this.data.material_no,
      material_name: this.data.material_name,
      order: this.data.order,
    }
    console.log("参数", data);
    this.post(url, data).then(res => {
      console.log("包含物料编码叫料", res)
      if (res.code == 20000) {

        wx.showToast({
          title: res.msg,
          icon: "success"
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  //  仅包含物料名称叫料​
  submit2() {

    let job_names = []
    job_names.push(this.data.material_name)
    let url = this.data.baseUrl + "/ask_material";
    let data = {
      company: this.data.company,
      manager: this.data.manager,
      line: this.data.line,
      operation: this.data.operation,
      job_names: job_names,
      order: this.data.order,
    }
    console.log("参数", data);
    this.post(url, data).then(res => {
      console.log("仅包含物料名称叫料​", res)
      if (res.code == 20000) {

        wx.showToast({
          title: res.msg,
          icon: "success"
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getFactoryData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})